package weifei.wx.community.service;


import com.auth0.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weifei.wx.community.entity.Adminer;
import weifei.wx.community.mapper.AdminerMapper;

@Service
public class AdminerService
{
    @Autowired
    private AdminerMapper adminerMapper;

    public Integer getAdminerIDByToken(String token)
    {
        return JWT.decode(token).getClaim("adminer_id").asInt();
    }


    public Adminer getAdminerByToken(String token)
    {
        var adminer_id = JWT.decode(token).getClaim("adminer_id").asInt();
        if (adminer_id == null)
            return null;
        return this.getAdminerByID(adminer_id);
    }


    public Adminer getAdminerByID(int adminerId)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("id = ?", adminerId);
        return this.adminerMapper.selectOneByQuery(queryWrapper);
    }


    public Adminer getAdminerByUsername(String username)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("username = ?", username);
        return this.adminerMapper.selectOneByQuery(queryWrapper);
    }

}





